//
//  SongsListSqureViewController.m
//  LuckyRadio
//
//  Created by Mr、J on 15/11/18.
//  Copyright © 2015年 chenjun. All rights reserved.
//

#import "SongsListSqureViewController.h"
#import "SongsListSqureCollectionViewCell.h"
#import "CustomerNetworking.h"
#import "SongsListSqureModel.h"
#import <MJRefresh.h>
#import <MBProgressHUD.h>
#import "SongsDetailPlayViewController.h"


#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define WIDTH_PRO(X) (X)*(SCREEN_WIDTH/375)

#define HEIGHT_PRO(Y) (Y)*(SCREEN_HEIGHT/667)
@interface SongsListSqureViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, MBProgressHUDDelegate>

@property (nonatomic, retain) UIView *totalView;

@property (nonatomic, retain) UIButton *HotestBtn;

@property (nonatomic, retain) UIButton *newestbtn;

@property (nonatomic, retain) UILabel *tagLabel;

@property (nonatomic, retain) NSMutableArray *dataArray;

@property (nonatomic, retain) UICollectionView *collectionView;

@property (nonatomic, retain) UICollectionView *collectionView2;

@property (nonatomic, retain) NSMutableArray *data2Array;

@property (nonatomic, assign) NSInteger page;

@property (nonatomic, assign) NSInteger page2;

@property (nonatomic, retain) MBProgressHUD *HUD;

@property (nonatomic, retain) MBProgressHUD *HUD2;

@end

@implementation SongsListSqureViewController
- (void)dealloc
{
    [_HUD release];
    [_dataArray release];
    [_totalView release];
    [_HotestBtn release];
    [_newestbtn release];
    [_collectionView release];
    [_collectionView2 release];
    [_data2Array release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.navigationItem.title = @"歌单广场";
    self.page = 1;
    self.page2 = 1;
    self.dataArray = [NSMutableArray array];
    self.data2Array = [NSMutableArray array];
    [self loadData];
    [self loadData2];
    [self createSubViews];

    
}

-(void)createSubViews
{
    self.totalView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HEIGHT_PRO(50))];
    [self.view addSubview:self.totalView];
    _totalView.backgroundColor = [UIColor whiteColor];
    [_totalView release];
    
    
    
    self.HotestBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.HotestBtn.frame = CGRectMake(SCREEN_WIDTH - WIDTH_PRO(100), HEIGHT_PRO(15), WIDTH_PRO(40), HEIGHT_PRO(20));
    [self.HotestBtn setTitle:@"最热" forState:UIControlStateNormal];
    self.HotestBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.HotestBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.HotestBtn addTarget:self action:@selector(HotestBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.totalView addSubview:self.HotestBtn];
    
    self.tagLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - WIDTH_PRO(60), HEIGHT_PRO(10), WIDTH_PRO(10), HEIGHT_PRO(30))];
    self.tagLabel.textColor = [UIColor grayColor];
    self.tagLabel.font = [UIFont systemFontOfSize:30];
    self.tagLabel.text = @"|";
    [self.totalView addSubview:self.tagLabel];
    [_tagLabel release];
    
    self.newestbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.newestbtn.frame = CGRectMake(SCREEN_WIDTH - WIDTH_PRO(55), HEIGHT_PRO(15), WIDTH_PRO(40), HEIGHT_PRO(20));
    [self.newestbtn setTitle:@"最新" forState:UIControlStateNormal];
    self.newestbtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.newestbtn addTarget:self action:@selector(newestbtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.newestbtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.totalView addSubview:self.newestbtn];
    
    
    UICollectionViewFlowLayout *collectionViewFlowLayout2 = [[UICollectionViewFlowLayout alloc] init];
    collectionViewFlowLayout2.itemSize = CGSizeMake((SCREEN_WIDTH - WIDTH_PRO(30)) / 2, (SCREEN_WIDTH - WIDTH_PRO(30)) / 2 + HEIGHT_PRO(50));
    self.collectionView2 = [[UICollectionView alloc] initWithFrame:CGRectMake(0, HEIGHT_PRO(50), SCREEN_WIDTH, SCREEN_HEIGHT - HEIGHT_PRO(100) ) collectionViewLayout:collectionViewFlowLayout2];
    self.collectionView2.backgroundColor = [UIColor whiteColor];
    self.collectionView2.dataSource = self;
    self.collectionView2.delegate = self;
    [self.view addSubview:self.collectionView2];
    [_collectionView2 release];
    [collectionViewFlowLayout2 release];
    
    
    
    
    
    
    UICollectionViewFlowLayout *collectionViewFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    collectionViewFlowLayout.itemSize = CGSizeMake((SCREEN_WIDTH - WIDTH_PRO(30)) / 2, (SCREEN_WIDTH - WIDTH_PRO(30)) / 2 + HEIGHT_PRO(50));
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, HEIGHT_PRO(50), SCREEN_WIDTH, SCREEN_HEIGHT - HEIGHT_PRO(100) ) collectionViewLayout:collectionViewFlowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    
    
    
                                
    
    
    self.collectionView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [self.collectionView.mj_footer beginRefreshing];
        _page = _page + 1;
        [self loadData];
    }];
    

    
    
    self.collectionView2.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [self.collectionView2.mj_footer beginRefreshing];
        _page2 = _page2 + 1;
        [self loadData2];
    }];
  

    
    
    [self.collectionView registerClass:[SongsListSqureCollectionViewCell class] forCellWithReuseIdentifier:@"songsListSqureCell"];
    
    [self.collectionView2 registerClass:[SongsListSqureCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

}

-(void)loadData
{
    if (_page == 1) {
        self.HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
        self.HUD.delegate = self;
        
        //显示文字
        self.HUD.labelText = @"正在进行加载~";
    }
    
    NSString *string1 = @"http://so.ard.iyyin.com/s/songlist?q=tag:%E6%9C%80%E7%83%AD";
    NSString *string2 = [NSString stringWithFormat:@"&page=%ld&size=10&app=ttpod&v=v8.1.1.2015110616&user_id=0&mid=iPhone5S&f=f234&s=s310&imsi=&hid=&splus=9.1&active=1&net=2&openudid=e97c1f17f916efa173065a10152c7506e65b60f7&idfa=5FBD2BF5-1484-4C9B-A138-4D9CE22EADB5&utdid=VkKezJUW5SwDAGga0yzrZnyO&alf", _page];
    NSString *string3 = @"=700606&bundle_id=com.ttpod.music&latitude=38.89680989464323&longtitude=121.5286713817766";
    NSString *string = [NSString stringWithFormat:@"%@%@%@", string1, string2, string3];
    
    
    [CustomerNetworking customerWithURL:string andParamer:nil andBlock:^(id response) {
        for (NSDictionary *dic in [response objectForKey:@"data"]) {
            SongsListSqureModel *songsListSqureModel = [SongsListSqureModel modelWithDictionary:dic];
            [self.dataArray addObject:songsListSqureModel];
        }
        [self.collectionView reloadData];
        [self.collectionView.mj_footer endRefreshing];
        [self.HUD hide:YES];
    }];
}

-(void)loadData2
{
    self.HUD2 = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
    self.HUD2.delegate = self;
    
    //常用的设置
    //小矩形的背景色
    // self.HUD.color = [UIColor clearColor];//这儿表示无背景
    //显示的文字
    self.HUD2.labelText = @"正在进行加载~";
    
    NSString *str1 = @"http://so.ard.iyyin.com/s/songlist?q=tag:%E6%9C%80%E6%96%B0";
    NSString *str2 = [NSString stringWithFormat:@"&page=%ld&size=10&app=ttpod&v=v8.1.1.2015110616&user_id=0&mid=iPhone5S&f=f234&s=s310&imsi=&hid=&splus=9.1&active=1", _page2];
    NSString *str3 = @"&net=2&openudid=e97c1f17f916efa173065a10152c7506e65b60f7&idfa=5FBD2BF5-1484-4C9B-A138-4D9CE22EADB5&utdid=VkKezJUW5SwDAGga0yzrZnyO&alf=700606&bundle_id=com.ttpod.music&latitude=38.89680989464323&longtitude=121.5286713817766";
    NSString *str = [NSString stringWithFormat:@"%@%@%@", str1, str2, str3];
    
    [CustomerNetworking customerWithURL:str andParamer:nil andBlock:^(id response) {
        for (NSDictionary *dic in [response objectForKey:@"data"]) {
            SongsListSqureModel *songsListSqureModel = [SongsListSqureModel modelWithDictionary:dic];
            [self.data2Array addObject:songsListSqureModel];
        }
        [self.collectionView2 reloadData];
        [self.collectionView2.mj_footer endRefreshing];
        [self.HUD2 hide:YES];
        
    }];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView == self.collectionView) {
        return _dataArray.count;
    }else{
        return _data2Array.count;
    }
}

-(UIEdgeInsets )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, WIDTH_PRO(10), 0, WIDTH_PRO(10));
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == self.collectionView) {
        SongsListSqureModel *songsListSqureModel = _dataArray[indexPath.item];
        SongsListSqureCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"songsListSqureCell" forIndexPath:indexPath];
        cell.songsListSqureModel = songsListSqureModel;
        return cell;
    }else{
        SongsListSqureModel *songsListSqureModel = _data2Array[indexPath.item];

        SongsListSqureCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        cell.songsListSqureModel = songsListSqureModel;
        return cell;
    }
}

-(void)HotestBtnAction:(UIButton *)sender
{
    [self.HotestBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.newestbtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.view insertSubview:self.collectionView aboveSubview:self.collectionView2];
}

-(void)newestbtnAction:(UIButton *)sender
{
    [self.newestbtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.HotestBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.view insertSubview:self.collectionView2 aboveSubview:self.collectionView];
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == self.collectionView) {
        SongsListSqureModel *songsListSqureModel = _dataArray[indexPath.item];
        SongsDetailPlayViewController *songsDetail = [[SongsDetailPlayViewController alloc] init];
        songsDetail.rankID = songsListSqureModel._id;
        [self.navigationController pushViewController:songsDetail animated:YES];
        
    }else{
        SongsListSqureModel *songsListSqureModel = _data2Array[indexPath.item];
        
        SongsDetailPlayViewController *songsDetail = [[SongsDetailPlayViewController alloc] init];
        songsDetail.rankID = songsListSqureModel._id;
        [self.navigationController pushViewController:songsDetail animated:YES];
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
